package powerws

import (
	"power/core/localwrap"
	"strconv"
	"sync"

	"github.com/RocksonZeta/wrap/rediswrap"
)

var powerNetworks sync.Map

var g = `
{
	"version": "2.0",
	"refs": {
		"24": {
			"_classPath": "Q.Position.CENTER_MIDDLE"
		}
	},
	"datas": [
		{
			"_className": "Q.Text",
			"json": {
				"name": "Network Organization Chart",
				"styles": {
					"label.font.size": 30,
					"label.font.family": "Times",
					"label.color": "#FFF",
					"label.background.color": "#2898E0",
					"label.background.gradient": {
						"_classPath": "Q.Gradient.LINEAR_GRADIENT_VERTICAL"
					},
					"label.padding": {
						"_className": "Q.Insets",
						"json": {
							"top": 7,
							"left": 15,
							"bottom": 7,
							"right": 15
						}
					},
					"label.radius": 20
				},
				"location": {
					"x": 800,
					"y": 90
				}
			}
		},
		{
			"_className": "Q.Node",
			"json": {
				"location": {
					"x": 100,
					"y": 100
				},
				"image": "./network/images/db.png"
			},
			"_refId": "172"
		},
		{
			"_className": "Q.Node",
			"json": {
				"location": {
					"x": 220,
					"y": 100
				},
				"image": "./network/images/ex1.png"
			},
			"_refId": "173"
		},
		{
			"_className": "Q.Node",
			"json": {
				"location": {
					"x": 333,
					"y": 100
				},
				"image": "./network/images/www.png"
			},
			"_refId": "174"
		},
		{
			"_className": "Q.Node",
			"json": {
				"location": {
					"x": 220,
					"y": 195
				},
				"image": "./network/images/ex2.png"
			},
			"_refId": "175"
		},
		{
			"_className": "Q.Node",
			"json": {
				"location": {
					"x": 95,
					"y": 270
				},
				"image": "./network/images/search.png"
			},
			"_refId": "176"
		},
		{
			"_className": "Q.Node",
			"json": {
				"name": "Fx/Tx",
				"location": {
					"x": 270,
					"y": 350
				},
				"image": "./network/images/FxTx.png"
			},
			"_refId": "177"
		},
		{
			"_className": "Q.Node",
			"json": {
				"location": {
					"x": 323,
					"y": 350
				},
				"image": "./network/images/icon1.png"
			},
			"_refId": "178"
		},
		{
			"_className": "Q.Node",
			"json": {
				"name": "Fx/Tx",
				"location": {
					"x": 375,
					"y": 350
				},
				"image": "./network/images/FxTx.png"
			},
			"_refId": "179"
		},
		{
			"_className": "Q.Node",
			"json": {
				"location": {
					"x": 125,
					"y": 430
				},
				"image": "./network/images/FxTx.png"
			},
			"_refId": "180"
		},
		{
			"_className": "Q.Node",
			"json": {
				"location": {
					"x": 125,
					"y": 522
				},
				"image": "./network/images/icon1.png"
			},
			"_refId": "181"
		},
		{
			"_className": "Q.Node",
			"json": {
				"name": "internet",
				"styles": {
					"label.font.size": 20,
					"label.font.style": "bold",
					"label.anchor.position": {
						"_classPath": "Q.Position.LEFT_MIDDLE"
					},
					"label.position": {
						"_classPath": "Q.Position.RIGHT_MIDDLE"
					}
				},
				"location": {
					"x": 300,
					"y": 577
				},
				"image": "./network/images/internet.png"
			},
			"_refId": "182"
		},
		{
			"_className": "Q.Node",
			"json": {
				"name": "Ethernet",
				"location": {
					"x": 500,
					"y": 350
				},
				"image": "./network/images/ex2.png"
			},
			"_refId": "183"
		},
		{
			"_className": "Q.Node",
			"json": {
				"location": {
					"x": 500,
					"y": 577
				},
				"image": "./network/images/server1.png"
			},
			"_refId": "184"
		},
		{
			"_className": "Q.Node",
			"json": {
				"location": {
					"x": 665,
					"y": 226
				},
				"image": "./network/images/ex2.png"
			},
			"_refId": "185"
		},
		{
			"_className": "Q.Node",
			"json": {
				"location": {
					"x": 665,
					"y": 296
				},
				"image": "./network/images/ex2.png"
			},
			"_refId": "186"
		},
		{
			"_className": "Q.Node",
			"json": {
				"location": {
					"x": 665,
					"y": 366
				},
				"image": "./network/images/ex2.png"
			},
			"_refId": "187"
		},
		{
			"_className": "Q.Node",
			"json": {
				"location": {
					"x": 665,
					"y": 436
				},
				"image": "./network/images/ex2.png"
			},
			"_refId": "188"
		},
		{
			"_className": "Q.Node",
			"json": {
				"location": {
					"x": 665,
					"y": 506
				},
				"image": "./network/images/ex2.png"
			},
			"_refId": "189"
		},
		{
			"_className": "Q.Node",
			"json": {
				"name": "Cloud",
				"styles": {
					"label.font.size": 30,
					"label.anchor.position": {
						"$ref": 24
					},
					"label.position": {
						"$ref": 24
					}
				},
				"location": {
					"x": 866,
					"y": 366
				},
				"image": "./network/images/cloud.png"
			},
			"_refId": "190"
		},
		{
			"_className": "Q.Edge",
			"json": {
				"name": "10BaseT",
				"styles": {
					"label.radius": 0,
					"label.rotatable": false,
					"label.background.color": "#FFFFFF",
					"label.anchor.position": {
						"$ref": 24
					},
					"arrow.to": false
				},
				"from": {
					"_ref": 172
				},
				"to": {
					"_ref": 175
				},
				"edgeType": "orthogonal.H"
			}
		},
		{
			"_className": "Q.Edge",
			"json": {
				"name": "10BaseTX",
				"styles": {
					"label.radius": 0,
					"label.rotatable": false,
					"label.background.color": "#FFFFFF",
					"label.anchor.position": {
						"$ref": 24
					},
					"arrow.to": false
				},
				"from": {
					"_ref": 173
				},
				"to": {
					"_ref": 175
				}
			}
		},
		{
			"_className": "Q.Edge",
			"json": {
				"name": "10BaseT",
				"styles": {
					"label.radius": 0,
					"label.rotatable": false,
					"label.background.color": "#FFFFFF",
					"label.anchor.position": {
						"$ref": 24
					},
					"arrow.to": false
				},
				"from": {
					"_ref": 174
				},
				"to": {
					"_ref": 175
				},
				"edgeType": "orthogonal.V.H"
			}
		},
		{
			"_className": "Q.Edge",
			"json": {
				"name": "10BaseT",
				"styles": {
					"label.radius": 0,
					"label.rotatable": false,
					"label.background.color": "#FFFFFF",
					"label.anchor.position": {
						"$ref": 24
					},
					"arrow.to": false,
					"edge.to.offset": {
						"x": -15
					}
				},
				"from": {
					"_ref": 176
				},
				"to": {
					"_ref": 175
				},
				"edgeType": "orthogonal.H.V"
			}
		},
		{
			"_className": "Q.Edge",
			"json": {
				"name": "10BaseTX",
				"styles": {
					"label.radius": 0,
					"label.rotatable": false,
					"label.background.color": "#FFFFFF",
					"label.anchor.position": {
						"$ref": 24
					},
					"arrow.to": false,
					"edge.to.offset": {
						"x": 15
					}
				},
				"from": {
					"_ref": 177
				},
				"to": {
					"_ref": 175
				},
				"edgeType": "orthogonal.H.V"
			}
		},
		{
			"_className": "Q.Edge",
			"json": {
				"name": "10BaseT",
				"styles": {
					"label.radius": 0,
					"label.rotatable": false,
					"label.background.color": "#FFFFFF",
					"label.anchor.position": {
						"$ref": 24
					},
					"arrow.to": false
				},
				"from": {
					"_ref": 180
				},
				"to": {
					"_ref": 175
				},
				"edgeType": "orthogonal.H.V"
			}
		},
		{
			"_className": "Q.Edge",
			"json": {
				"styles": {
					"label.radius": 0,
					"label.rotatable": false,
					"label.background.color": "#FFFFFF",
					"label.anchor.position": {
						"$ref": 24
					},
					"arrow.to": false
				},
				"from": {
					"_ref": 177
				},
				"to": {
					"_ref": 178
				}
			}
		},
		{
			"_className": "Q.Edge",
			"json": {
				"styles": {
					"label.radius": 0,
					"label.rotatable": false,
					"label.background.color": "#FFFFFF",
					"label.anchor.position": {
						"$ref": 24
					},
					"arrow.to": false
				},
				"from": {
					"_ref": 178
				},
				"to": {
					"_ref": 179
				}
			}
		},
		{
			"_className": "Q.Edge",
			"json": {
				"name": "Protocol Translator",
				"styles": {
					"label.radius": 0,
					"label.rotatable": false,
					"label.background.color": "#FFFFFF",
					"label.anchor.position": {
						"$ref": 24
					},
					"arrow.to": false
				},
				"from": {
					"_ref": 180
				},
				"to": {
					"_ref": 181
				}
			}
		},
		{
			"_className": "Q.Edge",
			"json": {
				"styles": {
					"label.radius": 0,
					"label.rotatable": false,
					"label.background.color": "#FFFFFF",
					"label.anchor.position": {
						"$ref": 24
					},
					"arrow.to": false
				},
				"from": {
					"_ref": 181
				},
				"to": {
					"_ref": 182
				},
				"edgeType": "orthogonal.V.H"
			}
		},
		{
			"_className": "Q.Edge",
			"json": {
				"name": "10BaseTX",
				"styles": {
					"label.radius": 0,
					"label.rotatable": false,
					"label.background.color": "#FFFFFF",
					"label.anchor.position": {
						"$ref": 24
					},
					"arrow.to": false
				},
				"from": {
					"_ref": 179
				},
				"to": {
					"_ref": 183
				}
			}
		},
		{
			"_className": "Q.Edge",
			"json": {
				"name": "10BaseTX",
				"styles": {
					"label.radius": 0,
					"label.rotatable": false,
					"label.background.color": "#FFFFFF",
					"label.anchor.position": {
						"$ref": 24
					},
					"arrow.to": false
				},
				"from": {
					"_ref": 183
				},
				"to": {
					"_ref": 184
				}
			}
		},
		{
			"_className": "Q.Edge",
			"json": {
				"name": "10BaseTX",
				"styles": {
					"label.radius": 0,
					"label.rotatable": false,
					"label.background.color": "#FFFFFF",
					"label.anchor.position": {
						"$ref": 24
					},
					"arrow.to": false
				},
				"from": {
					"_ref": 183
				},
				"to": {
					"_ref": 188
				},
				"edgeType": "orthogonal.H"
			}
		},
		{
			"_className": "Q.Edge",
			"json": {
				"name": "10BaseTX",
				"styles": {
					"label.radius": 0,
					"label.rotatable": false,
					"label.background.color": "#FFFFFF",
					"label.anchor.position": {
						"$ref": 24
					},
					"arrow.to": false
				},
				"from": {
					"_ref": 185
				},
				"to": {
					"_ref": 186
				}
			}
		},
		{
			"_className": "Q.Edge",
			"json": {
				"name": "10BaseTX",
				"styles": {
					"label.radius": 0,
					"label.rotatable": false,
					"label.background.color": "#FFFFFF",
					"label.anchor.position": {
						"$ref": 24
					},
					"arrow.to": false
				},
				"from": {
					"_ref": 186
				},
				"to": {
					"_ref": 187
				}
			}
		},
		{
			"_className": "Q.Edge",
			"json": {
				"name": "10BaseTX",
				"styles": {
					"label.radius": 0,
					"label.rotatable": false,
					"label.background.color": "#FFFFFF",
					"label.anchor.position": {
						"$ref": 24
					},
					"arrow.to": false
				},
				"from": {
					"_ref": 187
				},
				"to": {
					"_ref": 188
				}
			}
		},
		{
			"_className": "Q.Edge",
			"json": {
				"name": "10BaseTX",
				"styles": {
					"label.radius": 0,
					"label.rotatable": false,
					"label.background.color": "#FFFFFF",
					"label.anchor.position": {
						"$ref": 24
					},
					"arrow.to": false
				},
				"from": {
					"_ref": 188
				},
				"to": {
					"_ref": 189
				}
			}
		},
		{
			"_className": "Q.Edge",
			"json": {
				"styles": {
					"label.radius": 0,
					"label.rotatable": false,
					"label.background.color": "#FFFFFF",
					"label.anchor.position": {
						"$ref": 24
					},
					"arrow.to": false
				},
				"from": {
					"_ref": 185
				},
				"to": {
					"_ref": 190
				}
			}
		},
		{
			"_className": "Q.Edge",
			"json": {
				"styles": {
					"label.radius": 0,
					"label.rotatable": false,
					"label.background.color": "#FFFFFF",
					"label.anchor.position": {
						"$ref": 24
					},
					"arrow.to": false
				},
				"from": {
					"_ref": 186
				},
				"to": {
					"_ref": 190
				}
			}
		},
		{
			"_className": "Q.Edge",
			"json": {
				"styles": {
					"label.radius": 0,
					"label.rotatable": false,
					"label.background.color": "#FFFFFF",
					"label.anchor.position": {
						"$ref": 24
					},
					"arrow.to": false
				},
				"from": {
					"_ref": 187
				},
				"to": {
					"_ref": 190
				}
			}
		},
		{
			"_className": "Q.Edge",
			"json": {
				"styles": {
					"label.radius": 0,
					"label.rotatable": false,
					"label.background.color": "#FFFFFF",
					"label.anchor.position": {
						"$ref": 24
					},
					"arrow.to": false
				},
				"from": {
					"_ref": 188
				},
				"to": {
					"_ref": 190
				}
			}
		},
		{
			"_className": "Q.Edge",
			"json": {
				"styles": {
					"label.radius": 0,
					"label.rotatable": false,
					"label.background.color": "#FFFFFF",
					"label.anchor.position": {
						"$ref": 24
					},
					"arrow.to": false
				},
				"from": {
					"_ref": 189
				},
				"to": {
					"_ref": 190
				}
			}
		}
	],
	"scale": 0.6722771287766852,
	"tx": 51.39181401637352,
	"ty": 38.70464695454666
}`

func GetOrNewPowerNetwork(id int) (*PowerNetwork, error) {
	log.Trace().Func("powerNetworks").Int("id", id).Send()
	if g, ok := powerNetworks.Load(id); ok {
		return g.(*PowerNetwork), nil
	}
	options := PowerNetworkOptions{
		Id: id,
	}
	options.RedisChannel = "PowerNetwork/" + strconv.Itoa(id)
	options.ChannelSize = 1
	redisOptions, err := rediswrap.ParseUrl(localwrap.Config.Redis)
	if err != nil {
		return nil, err
	}
	options.Redis = *redisOptions
	g, err := NewPowerNetwork(options)
	if err != nil {
		return g, err
	}
	// g.graphJson = `{"version":"2.0","datas":[{"_className":"Q.Node","json":{"name":"Qunee","location":{"x":0,"y":0}},"_refId":"171"},{"_className":"Q.Node","json":{"name":"node1","location":{"x":100,"y":100}},"_refId":"172"},{"_className":"Q.Node","json":{"name":"hello","location":{"x":-100,"y":100}},"_refId":"173"},{"_className":"Q.Edge","json":{"name":"Hello\nQunee","from":{"_ref":173},"to":{"_ref":171}}},{"_className":"Q.Edge","json":{"name":"1->2","from":{"_ref":173},"to":{"_ref":172}}}],"scale":1,"tx":632,"ty":250}`
	powerNetworks.Store(id, g)
	return g, nil
}
